// Show time of client PC.
function localclock(){
	now = new Date();

	hh = now.getHours() ;	mm = now.getMinutes() ;	ss = now.getSeconds() ;

	if (hh < 10) { hh = "0" + hh }
	if (mm < 10) { mm = "0" + mm }
	if (ss < 10) { ss = "0" + ss }

	//window.status = now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getUTCDate() + "  " + hh + ":" + mm + ":" + ss ;
	window.status = now.getUTCDate() + "/" + (now.getMonth() + 1) + "/" + now.getFullYear() + "  " + hh + ":" + mm + ":" + ss ;
	setTimeout('localclock()', 1000) ;
}
localclock();

// Change background color when focus out.
function textOnBlur() {
	window.event.srcElement.style.backgroundColor = "white" ;
}

// Change background color when focus in.
function textOnFocus() {
	window.event.srcElement.style.backgroundColor = "lightskyblue" ;
}

// Initialize function.
// 1.Set focus.
// 2.Show message dialog.
function init(msg, focusedField,formid){
//	setTimeout('sessionWarning()',780000);
	window.history.forward();
	window.focus();
	initFocus(focusedField,formid);
	showAlert(msg);
}

//function sessionWarning(){
//	alert("Session time will expire within 2 minutes.");
//}

function showAlert(msg){
	if(msg != ""){
		alert(msg);
	}
}

function initFocus(focusedField,formid){
		window.history.forward();
		window.focus();
		if(focusedField != "") {
		    eval("document.getElementById('"+formid+"')." + focusedField + ".focus()");
		}
}

//function for CommonDialogSearch
var subWin = null;
function openDialog(serviceid,blclassname,eventname,arg4,arg5,arg6,arg7,arg8,arg9,arg10){  
		
        var width = 0;
        var height = 0 ;
        var resizeable=0;
        
        if (serviceid == 'commoditysearchdialog'){
       		width=500;
       		height=400;
      		resizeable=1;
      		if(subWin == null || subWin.closed) {   
		 		 subWin = window.open(serviceid + ".do?blClassName=" + blclassname + "&&eventName=" + eventname + "&&inputField=" + arg4 + "&&inputField=" + arg5 + "&&flagCheckTextBox=" + arg6 + "&&clientCode="+ arg7 +"&&formId="+arg8,"","height="+height+",width="+width+",resizeable="+resizeable+",scrollbars=1");   
	   		}
	 	    subWin.focus();
         }
        else if(serviceid == 'lotsearchdialog'){
        	width=770;
        	height=480;
        	resizeable=1;
        	if(subWin == null || subWin.closed) {
		       subWin = window.open(serviceid + ".do?blClassName=" + blclassname + "&&eventName=" + eventname + "&&inputField=" + arg4 + "&&inputField=" + arg5 + "&&inputField=" + arg9 + "&&inputField=" + arg10 + "&&flagCheckTextBox=" + arg6 + "&&clientCode="+ arg7 +"&&formId="+arg8,"","height="+height+",width="+width+",resizeable="+resizeable+",scrollbars=1");   
	   		}
	  		subWin.focus();
         } 
         if(serviceid == 'permittedtypedialog'){
        	width=600;
        	height=480;
        	resizeable=1;
        	if(subWin == null || subWin.closed) {   
		 		 subWin = window.open(serviceid + ".do?blClassName=" + blclassname + "&&eventName=" + eventname + "&&inputField=" + arg4 + "&&inputField=" + arg5 + "&&flagCheckTextBox=" + arg6 + "&&clientCode="+ arg7 +"&&commodityType="+arg8+ "&&formId="+ arg9 ,"","height="+height+",width="+width+",resizeable="+resizeable+",scrollbars=1");   
	   		}
	  		subWin.focus();
         } 
}

function closeWinSub(){
	if(subWin) {
		subWin.close();
	}
}


//Saran add for select Dialog value
	function sentValue(tmp,formId) {
		
		var sInputFieldId;
		var isFocus = false;
		
		tmp = tmp.split(':');
		
		for(var i = 0; i < tmp.length; i++) {
			sInputFieldId = document.forms[0].inputField[i].value;
			/* get input object */
			var oInput = window.opener.document.getElementById(sInputFieldId);
			if(!oInput){
				continue;
			}
			/* set value */
			oInput.value = tmp[i];
			if(!isFocus){
        		oInput.focus();
        		isFocus = true;
        	}
		
		}
		window.close();
	}
	
	//saran add for close dialog click
	
	function closeDialog(formId){
		value = document.forms[0].inputField[0].value;
		eval("window.opener.document.getElementById('"+formId+"')." + value + ".focus()");
		window.close();
	}
	
	function isEmpty(val){
		if(val.length == 0){
			return true;
		}else{
		    return false;
		}
	
	}	
	
	function submitform(){
    	document.forms[0].submit() ;	
	}
	
	function openPopup(divId, fn) {
	    jQuery.fancybox( {
	        'href' : '#' + divId, 'titlePosition' : 'inside', 'transitionIn' : 'fade', 'transitionOut' : 'fade', 'showCloseButton' : false, 'hideOnOverlayClick' : false, 'centerOnScroll' : true, 'onComplete' : fn
	    });
	}

	function closePopup() {
	    jQuery.fancybox.close();
	}

	function g_isTime(i_string) {
	    //Return True  = is Time Format (HH24:MI);
	    //Return False = isn't Time Format;
	    i_string = g_wtrim(i_string);
	    var v_time = i_string.split(":");

	    if (v_time.length != 2) {
	        return false;
	    }

	    var v_hour = v_time[0];
	    var v_min = v_time[1];

	    if ((v_hour.length != 2) || (v_min.length != 2)) {
	        return false;
	    }

	    for (var i = 0;i < v_time.length;i++) {
	        if (!g_isNumber(v_time[i])) {
	            return false;
	        }
	    }

	    v_hour = parseInt(v_hour, 10);
	    v_min = parseInt(v_min, 10);

	    if ((v_hour < 0 || v_hour > 23) || (v_min < 0 || v_min > 59)) {
	        return false;
	    }
	    return true;
	}

	function g_isNumber(i_string) {
	    //Input String
	    //Return True => StringInput is number
	    //Return False => StringInput isn't number
	    var v_length = i_string.length;
	    var v_status2 = true;

	    for (var k = 0;k < v_length;k++) {
	        if (v_status2) {
	            if (i_string.charAt(0) == '-') {
	                //k
	                v_status2 = false;
	                continue;
	            }
	        }
	        if ((i_string.charAt(k) < '0') || (i_string.charAt(k) > '9')) {
	            return false;
	        }
	    }
	    return true;
	}

	function g_wtrim(i_str) {
	    return g_trim(i_str, " ");
	}

	function g_trim(i_str, i_chars) {
	    return g_ltrim(g_rtrim(i_str, i_chars), i_chars);
	}

	function g_ltrim(i_str, i_chars) {
	    i_chars = i_chars || "\\s";
	    return i_str.replace(new RegExp("^[" + i_chars + "]+", "g"), "");
	}

	function g_rtrim(i_str, i_chars) {
	    i_chars = i_chars || "\\s";
	    return i_str.replace(new RegExp("[" + i_chars + "]+$", "g"), "");
	}

	function converttoSqlDate(date) {
	    var dateOut = date.split("/");

	    var year = dateOut[2] - 543;
	    var month = dateOut[1];
	    var day = dateOut[0];

	    return year + "-" + month + "-" + day;
	}

	function openCalendar(divId, toDay) {
	    var d = new Date();
	    if (toDay == undefined)
	        toDay = d.getDate() + '/' + (d.getMonth() + 1) + '/' + (d.getFullYear() + 543);
	    jQuery("#" + divId).datepicker( {
	        dateFormat : 'dd/mm/yy', isBuddhist : true, defaultDate : toDay, showOn : 'button', buttonImage : './image/calendar_view.png', buttonImageOnly : true, dayNames : ['เธญเธฒเธ—เธดเธ•เธขเน�', 'เธ�เธฑเธ�เธ—เธฃเน�', 'เธญเธฑเธ�เธ�เธฒเธฃ', 'เธ�เธธเธ�', 'เธ�เธคเธซเธฑเธชเธ�เธ”เธต', 'เธจเธธเธ�เธฃเน�', 'เน€เธชเธฒเธฃเน�'], dayNamesMin : ['เธญเธฒ.', 'เธ�.', 'เธญ.', 'เธ�.', 'เธ�เธค.', 'เธจ.', 'เธช.'], monthNames : ['เธกเธ�เธฃเธฒเธ�เธก', 'เธ�เธธเธกเธ เธฒเธ�เธฑเธ�เธ�เน�', 'เธกเธตเธ�เธฒเธ�เธก', 'เน€เธกเธฉเธฒเธขเธ�', 'เธ�เธคเธฉเธ เธฒเธ�เธก', 'เธกเธดเธ–เธธเธ�เธฒเธขเธ�', 'เธ�เธฃเธ�เธ�เธฒเธ�เธก', 'เธชเธดเธ�เธซเธฒเธ�เธก', 'เธ�เธฑเธ�เธขเธฒเธขเธ�', 'เธ•เธธเธฅเธฒเธ�เธก', 'เธ�เธคเธจเธ�เธดเธ�เธฒเธขเธ�', 'เธ�เธฑเธ�เธงเธฒเธ�เธก'], monthNamesShort : ['เธก.เธ�.', 'เธ�.เธ�.', 'เธกเธต.เธ�.', 'เน€เธก.เธข.', 'เธ�.เธ�.', 'เธกเธด.เธข.', 'เธ�.เธ�.', 'เธช.เธ�.', 'เธ�.เธข.', 'เธ•.เธ�.', 'เธ�.เธข.', 'เธ�.เธ�.'], changeMonth : true, changeYear : true
	    });
	    //    jQuery('#'+divId).val(toDay);
	    return toDay;

	}

	function addCommas(nStr) {
	    nStr += '';
	    var x = nStr.split('.');
	    var x1 = x[0];
	    var x2 = x.length > 1 ? '.' + x[1] : '';
	    var rgx = /(\d+)(\d{3})/;
	    while (rgx.test(x1)) {
	        x1 = x1.replace(rgx, '$1' + ',' + '$2');
	    }
	    return x1 + x2;
	}

	function removeCommas(str) {
	    return(str.replace(/,/g,''));
	}

	function g_isThai(ch) {
	    //Input String
	    //Return True => StringInputElement is Thai
	    //Return False => StringInputElement isn't Thai
	    var digit;

	    for (var i = 0;i < ch.length;i++) {
	        digit = ch.charAt(i);
	        if (((digit >= "a" && digit <= "z") || (digit >= "0" && digit <= "9") || (digit >= "A" && digit <= "Z"))) {
	            return false;
	        }
	    }
	    return true;
	}

	function g_alPha(i_string) {
	    //Input String
	    //Return True => StringInputElement is Alpha
	    //Return False => StringInputElement isn't Alpha
	    var v_alPha = /[\%\;\'\\\!\@\#\$\^\&\*\(\)\-\+\=\{\}\[\]\:\"\/\?\>\<\|\เธฟ]/;
	    if (v_alPha.test(i_string)) {
	        return true;
	    }
	    else {
	        return false;
	    }
	}

	function redirect(url){
	
		if(url != ''){
			window.location = genUrlToken(url);
		}
	}
	
	function genUrlToken(url){
		var token = 'token=' + Math.floor(Math.random()*10000000000);
		if(url.indexOf('?') != -1){
			token = '&'+token;
		}else{
			token = '?'+token;
		}
		return url+token;
	}
	
	function g_onPrice(i_string){
		
	}
	
	function g_isPrice(event) {
	    if(event.keyCode > 47 && event.keyCode < 58) { return true; } else
        if(event.keyCode == 46) { return true;}
	    
	    event.returnValue = false;
	}
	
	function getCurrentDate() {
		var date = new Date();
		var date_str=('0'+date.getDate()).substr(-2,2)+'/'+(1+date.getMonth() + '').substr(-2,2)+'/'+(date.getFullYear());
		return date_str;
	}
	
	function getTomorrow() {
		var date 		= new Date();
		var tomorrow 	= new Date(date.getTime() + 24 * 60 * 60 * 1000);
		var date_str	= ('0'+tomorrow .getDate()).substr(-2,2)+'/'+(1+tomorrow .getMonth() + '').substr(-2,2)+'/'+(tomorrow .getFullYear());
		return date_str;
	}
	
	function toDate(date) { 
		var year = date.substring(6,10);
		var month = date.substring(3,5);
		var day = date.substring(0,2);
		
		return new Date(year + "-" + month + "-" + day);
	}
	
	function g_setLoading() {
	    //	document.getElementsByName("div_loading").className = "loading-visible";
	    document.getElementById("div_loading").className = "loading-visible";
	}

	function g_unsetLoading() {
	    document.getElementById("div_loading").className = "loading-invisible";
	    //document.getElementById('block-page').style.display='none';
	}

	function g_setBlock() {
	    var D = document;
	    jQuery("#block-page").css("height", Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight)));
	    jQuery("#block-page").css("width", Math.max(Math.max(D.body.scrollWidth, D.documentElement.scrollWidth), Math.max(D.body.offsetWidth, D.documentElement.offsetWidth), Math.max(D.body.clientWidth, D.documentElement.clientWidth)));
	   // D.getElementById("mouse_focus_block").focus();
	    D.getElementById("block-page").className = "loading-block";
	}

	function g_unsetBlock() {
	    document.getElementById("block-page").className = "loading-unblock";
	}

	function loading() {
	    g_setLoading();
	    g_setBlock();
		jQuery("#div_loading").css("z-index","1111");
		jQuery("#block-page").css("z-index","1110");
	}

	function unloading() {
	    g_unsetLoading();
	    g_unsetBlock();
	}
	
	function isNumber(evt) {
	    evt = (evt) ? evt : window.event;
	    var charCode = (evt.which) ? evt.which : evt.keyCode;
	if(charCode == 46 ) return true; else
	    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	        return false;
	    }
	    return true;
	}
	
	function isFloat(evt, value) {
	    evt = (evt) ? evt : window.event;
	    var charCode = (evt.which) ? evt.which : evt.keyCode;
		if(charCode == 46 && value.indexOf(".") == -1) return true; else
		if(charCode == 44 && value.indexOf(".") == -1) return true; else
	    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	        return false;
	    }
	    return true;
	}
	
	function lpad(content,padLength,padChar)
	{
		content += "";

		//Length
	 	if(content.length > padLength){
			content = content.substring(0,padLength)
	 	}

	 	for(var i=content.length; i<padLength; i++){
			content = padChar + content;
	 	}
	 	
	 	return content;
	}